package chan.project.smartlawdatamanage.service.impl;

import chan.project.smartlawcommon.common.ErrorCode;
import chan.project.smartlawcommon.constant.CommonConstant;
import chan.project.smartlawcommon.exception.ThrowUtils;
import chan.project.smartlawcommon.utils.SqlUtils;
import chan.project.smartlawdatamanage.service.LawRegulationService;
import chan.project.smartlawdatamanage.mapper.LawRegulationMapper;
import chan.project.smartlawmodel.dto.lawregulation.LawRegulationQueryRequest;
import chan.project.smartlawmodel.entity.LawRegulation;
import chan.project.smartlawmodel.vo.LawRegulationVO;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 法律法规信息服务实现
 *
 * @author <a href="https://github.com/kavansp">kavansp</a>
 * @from <a href="https://www.code-nav.cn">编程导航学习圈</a>
 */
@Service
@Slf4j
public class LawRegulationServiceImpl extends ServiceImpl<LawRegulationMapper, LawRegulation>
        implements LawRegulationService {

    /**
     * 校验数据
     *
     * @param lawRegulation
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validLawRegulation(LawRegulation lawRegulation, boolean add) {
        ThrowUtils.throwIf(lawRegulation == null, ErrorCode.PARAMS_ERROR);
        String title = lawRegulation.getTitle();
        Date effectiveDate = lawRegulation.getEffectiveDate();
        Date promulgationDate = lawRegulation.getPromulgationDate();
        String content = lawRegulation.getContent();

        // 创建数据时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isBlank(title), ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(effectiveDate.before(promulgationDate),ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(StringUtils.isBlank(content),ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
    }

    /**
     * 获取查询条件
     *
     * @param lawRegulationQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<LawRegulation> getQueryWrapper(LawRegulationQueryRequest lawRegulationQueryRequest) {
        QueryWrapper<LawRegulation> queryWrapper = new QueryWrapper<>();
        if (lawRegulationQueryRequest == null) {
            return queryWrapper;
        }
        Long id = lawRegulationQueryRequest.getId();
        Long notId = lawRegulationQueryRequest.getNotId();
        String title = lawRegulationQueryRequest.getTitle();
        String sortField = lawRegulationQueryRequest.getSortField();
        String sortOrder = lawRegulationQueryRequest.getSortOrder();

        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        // 精确查询
        queryWrapper.ne(ObjectUtils.isNotEmpty(notId), "id", notId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 分页获取法律法规信息封装
     *
     * @param lawRegulationPage
     * @param request
     * @return
     */
    @Override
    public Page<LawRegulationVO> getLawRegulationVOPage(Page<LawRegulation> lawRegulationPage, HttpServletRequest request) {
        List<LawRegulation> lawRegulationList = lawRegulationPage.getRecords();
        Page<LawRegulationVO> lawRegulationVOPage = new Page<>(lawRegulationPage.getCurrent(), lawRegulationPage.getSize(), lawRegulationPage.getTotal());
        if (CollUtil.isEmpty(lawRegulationList)) {
            return lawRegulationVOPage;
        }
        // 对象列表 => 封装对象列表
        List<LawRegulationVO> lawRegulationVOList = lawRegulationList.stream().map(lawRegulation -> {
            return LawRegulationVO.objToVo(lawRegulation);
        }).collect(Collectors.toList());
        lawRegulationVOPage.setRecords(lawRegulationVOList);
        return lawRegulationVOPage;
    }
}
